package com.mobilesalon.provider;

import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.net.Uri;
import android.text.TextUtils;
import android.util.Log;

import com.mobilesalon.MobileSalonFactory;
import com.mobilesalon.provider.ServiceClientContract.Appointment;
import com.mobilesalon.provider.ServiceClientContract.Service;
import com.mobilesalon.provider.ServiceClientContract.ServiceClient;

public class ServiceClientProvider extends ContentProvider {
	private static final String TAG = ServiceClientProvider.class.getName();

	public static final String AUTHORITY = "com.mobilesalon.provider.ServiceClientProvider";

	private static final int PATH_SERVICE_NUM = 1;
	private static final int PATH_SERVICE_ID_NUM = 2;

	private static final int PATH_SERVICE_CLIENT_NUM = 3;
	private static final int PATH_SERVICE_CLIENT_ID_NUM = 4;

	private static final int PATH_APPOINTMENT_NUM = 5;
	private static final int PATH_APPOINTMENT_ID_NUM = 6;

	private DBHelper db;

	private static final UriMatcher URIMatcher = buildUriMatcher();

	/**
	 * Builds up a UriMatcher for search suggestion and shortcut refresh
	 * queries.
	 */
	private static UriMatcher buildUriMatcher() {
		UriMatcher matcher = new UriMatcher(UriMatcher.NO_MATCH);
		matcher.addURI(AUTHORITY, ServiceClient.PATH_URI, PATH_SERVICE_CLIENT_NUM);
		matcher.addURI(AUTHORITY, ServiceClient.PATH_URI + "/#", PATH_SERVICE_CLIENT_ID_NUM);
		matcher.addURI(AUTHORITY, Service.PATH_URI, PATH_SERVICE_NUM);
		matcher.addURI(AUTHORITY, Service.PATH_URI + "/#", PATH_SERVICE_ID_NUM);
		matcher.addURI(AUTHORITY, Appointment.PATH_URI, PATH_APPOINTMENT_NUM);
		matcher.addURI(AUTHORITY, Appointment.PATH_URI + "/#", PATH_APPOINTMENT_ID_NUM);

		return matcher;
	}

	@Override
	public String getType(Uri uri) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public boolean onCreate() {
		//this is required because content provider is initialized prior to application
		MobileSalonFactory.context = getContext();
		db = MobileSalonFactory.getDBHelper();
		MobileSalonFactory.context = null;
		return true;
	}

	@Override
	public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs,
			String sortOrder) {
		Cursor c = null;
		String id;
		switch (URIMatcher.match(uri)) {
		case PATH_SERVICE_CLIENT_NUM:
			Log.i(TAG, "Query service all");
			c = db.getDb().query(Tables.SERVICE_CLIENT_TABLE, projection, selection, selectionArgs,
					null, null, sortOrder);
			break;
		case PATH_SERVICE_CLIENT_ID_NUM:
			id = uri.getLastPathSegment();
			Log.i(TAG, "Query service id:" + id);
			c = db.getDb().query(Tables.SERVICE_CLIENT_TABLE, projection, "_ID =" + id, null, null,
					null, null);
			break;
		case PATH_SERVICE_NUM:
			Log.i(TAG, "Query services");
			c = db.getDb().query(Tables.SERVICE_TABLE, projection, selection, selectionArgs, null,
					null, sortOrder);
			break;
		case PATH_SERVICE_ID_NUM:
			id = uri.getLastPathSegment();
			Log.i(TAG, "Query service id:" + id);
			c = db.getDb().query(Tables.SERVICE_TABLE, projection, "_ID =" + id, null, null, null,
					null);
			break;
		case PATH_APPOINTMENT_NUM:
			Log.i(TAG, "Query appointments");
			c = db.getDb().query(Tables.APPOINTMENT_TABLE, projection, selection, selectionArgs,
					null, null, sortOrder);
			break;
		case PATH_APPOINTMENT_ID_NUM:
			id = uri.getLastPathSegment();
			Log.i(TAG, "Query appointment id:" + id);
			c = db.getDb().query(Tables.APPOINTMENT_TABLE, projection, "_ID =" + id, null, null,
					null, null);
			break;
		default:
			String warn = "Unknown URI " + uri;
			Log.w(TAG, warn);
			throw new IllegalArgumentException(warn);
		}

		return c;
	}

	@Override
	public Uri insert(Uri uri, ContentValues values) {
		Long rowId;
		switch (URIMatcher.match(uri)) {
		case PATH_SERVICE_CLIENT_NUM:
			Log.i(TAG, "insert service client");
			rowId = db.getDb().insert(Tables.SERVICE_CLIENT_TABLE, null, values);
			break;
		case PATH_SERVICE_NUM:
			Log.i(TAG, "insert service");
			rowId = db.getDb().insert(Tables.SERVICE_TABLE, null, values);
			break;
		case PATH_APPOINTMENT_NUM:
			Log.i(TAG, "insert appointment");
			rowId = db.getDb().insert(Tables.APPOINTMENT_TABLE, null, values);
			break;
		default:
			String warn = "Unknown URI " + uri;
			Log.w(TAG, warn);
			throw new IllegalArgumentException(warn);
		}
		return ContentUris.withAppendedId(uri, rowId);
	}

	@Override
	public int update(Uri uri, ContentValues values, String where, String[] whereArgs) {
		int count;
		switch (URIMatcher.match(uri)) {
		case PATH_SERVICE_ID_NUM:
			count = db.getDb().update(Tables.SERVICE_TABLE, values, where, whereArgs);
			break;
		case PATH_SERVICE_CLIENT_ID_NUM:
			count = db.getDb().update(Tables.SERVICE_CLIENT_TABLE, values, where, whereArgs);
			break;
		case PATH_APPOINTMENT_ID_NUM:
			count = db.getDb().update(Tables.APPOINTMENT_TABLE, values, where, whereArgs);
			break;
		default:
			throw new IllegalArgumentException("Unknown URI " + uri);
		}
		return count;
	}

	@Override
	public int delete(Uri uri, String whereClause, String[] whereArgs) {
		int count;
		String idToDelete;

		switch (URIMatcher.match(uri)) {
		case PATH_SERVICE_NUM:
			Log.i(TAG, "delete all services");
			count = db.getDb().delete(Tables.SERVICE_TABLE, whereClause, whereArgs);
			break;
		case PATH_SERVICE_ID_NUM:
			idToDelete = uri.getLastPathSegment();
			Log.i(TAG, "delete service:" + idToDelete);

			count = db.getDb()
					.delete(Tables.SERVICE_TABLE,
							"_ID ="
									+ idToDelete
									+ (!TextUtils.isEmpty(whereClause) ? " AND (" + whereClause
											+ ')' : ""), whereArgs);
			break;
		case PATH_SERVICE_CLIENT_NUM:
			Log.i(TAG, "delete all services client");
			count = db.getDb().delete(Tables.SERVICE_CLIENT_TABLE, whereClause, whereArgs);
			break;
		case PATH_SERVICE_CLIENT_ID_NUM:
			idToDelete = uri.getLastPathSegment();
			Log.i(TAG, "delete service client:" + idToDelete);

			count = db.getDb()
					.delete(Tables.SERVICE_CLIENT_TABLE,
							"_ID ="
									+ idToDelete
									+ (!TextUtils.isEmpty(whereClause) ? " AND (" + whereClause
											+ ')' : ""), whereArgs);
			break;
		case PATH_APPOINTMENT_NUM:
			Log.i(TAG, "delete all services client");
			count = db.getDb().delete(Tables.APPOINTMENT_TABLE, whereClause, whereArgs);
			break;
		case PATH_APPOINTMENT_ID_NUM:
			idToDelete = uri.getLastPathSegment();
			Log.i(TAG, "delete service client:" + idToDelete);

			count = db.getDb()
					.delete(Tables.APPOINTMENT_TABLE,
							"_ID ="
									+ idToDelete
									+ (!TextUtils.isEmpty(whereClause) ? " AND (" + whereClause
											+ ')' : ""), whereArgs);
			break;

		default:
			String warn = "Unknown URI " + uri;
			Log.w(TAG, warn);
			throw new IllegalArgumentException(warn);
		}
		return count;
	}

}
